package com.wmlive.hhvideo.common.navigator;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.wmlive.hhvideo.common.MyAppActivityManager;
import com.wmlive.hhvideo.heihei.beans.login.UserInfo;
import com.wmlive.hhvideo.heihei.beans.main.MultiTypeVideoBean;
import com.wmlive.hhvideo.heihei.beans.main.ShareInfo;
import com.wmlive.hhvideo.heihei.bells.activity.BellsActivity;
import com.wmlive.hhvideo.heihei.discovery.activity.TopicActivity;
import com.wmlive.hhvideo.heihei.mainhome.activity.VideoListActivity;
import com.wmlive.hhvideo.heihei.mainhome.fragment.RecommendFragmentNew;
import com.wmlive.hhvideo.heihei.personal.activity.AddFriendActivity;
import com.wmlive.hhvideo.heihei.personal.activity.DraftBoxActivity;
import com.wmlive.hhvideo.heihei.personal.activity.FansActivity;
import com.wmlive.hhvideo.heihei.personal.activity.FocusActivity;
import com.wmlive.hhvideo.heihei.personal.activity.PersonalHomeActivity;
import com.wmlive.hhvideo.heihei.personal.activity.PersonalInfoActivity;
import com.wmlive.hhvideo.heihei.personal.activity.PhotoCutActivity;
import com.wmlive.hhvideo.heihei.personal.activity.PhotoPicketActivity;
import com.wmlive.hhvideo.heihei.personal.activity.WebViewActivity;
import com.wmlive.hhvideo.utils.KLog;

/**
 * Created by vhawk on 2017/5/19.
 */

public class Navigator {

    private volatile static Navigator instance;

    public static Navigator getInstance() {
        KLog.i("Navigator初始化");
        if (instance == null) {
            synchronized (Navigator.class) {
                if (instance == null)
                    instance = new Navigator();//instance为volatile，现在没问题了
            }
        }
        return instance;
    }

    private Navigator() {
    }

    /**
     * 跳转到他人主页
     * 跳转到个人主页 如自己的视频头像等
     *
     * @param context
     * @param user_id
     */
    public void navigateToOtherUserHome(Context context, boolean fromComment, long user_id) {
        Intent intent = PersonalHomeActivity.getCallingIntent(context, fromComment, user_id);
        context.startActivity(intent);
    }

    /**
     * 跳转到添加好友
     *
     * @param context
     */
    public void navigateToAddFriend(Context context, ShareInfo shareInfo) {
        Intent intent = AddFriendActivity.getCallingIntent(context, shareInfo);
        context.startActivity(intent);
    }

    /**
     * 跳转到草稿箱
     *
     * @param context
     */
    public void navigateToAraftBox(Context context) {
        Intent intent = DraftBoxActivity.getCallingIntent(context);
        context.startActivity(intent);
    }

    /**
     * 跳转到关注列表
     *
     * @param context
     * @param userInfo
     */
    public void navigateToFocus(Context context, UserInfo userInfo) {
        Intent intent = FocusActivity.getCallingIntent(context, userInfo);
        context.startActivity(intent);
    }

    /**
     * 跳转到粉丝列表
     *
     * @param context
     * @param userInfo
     */
    public void navigateToFans(Context context, UserInfo userInfo) {
        Intent intent = FansActivity.getCallingIntent(context, userInfo);
        context.startActivity(intent);
    }

    /**
     * 跳转到个人信息
     *
     * @param context
     * @param userInfo
     */
    public void navigateToUserInfo(Context context, UserInfo userInfo) {
        Intent intent = PersonalInfoActivity.getCallingIntent(context, userInfo);
        context.startActivity(intent);
    }

    /**
     * 跳转到选择图片
     *
     * @param context
     */
    public void navigateToSelectPhoto(Context context) {
        Intent intent = PhotoPicketActivity.getCallingIntent(context);
        context.startActivity(intent);
    }

    /**
     * 跳转到图片裁剪
     *
     * @param context
     */
    public void navigateToPhotoCut(Context context, String path) {
        Intent intent = PhotoCutActivity.getCallingIntent(context, path);
        context.startActivity(intent);
    }

    /**
     * 轮播图的跳转
     * 例如    hhvideo://opus/detail?id=10001
     * scheme：hhvideo
     * host：opus
     * path：/detail
     * Query：id=10001
     * QueryParameter:10001
     *
     * @param deepLink
     */
    public void bannerLink(Activity ctx, String deepLink) {
        if (!TextUtils.isEmpty(deepLink)) {
            Uri uri = Uri.parse(deepLink);
            if (uri != null && "hhvideo".equals(uri.getScheme())) {
                switch (uri.getHost()) {
                    case "home"://首页
                        MyAppActivityManager.getInstance().finishAllActivityBefore(com.wmlive.hhvideo.heihei.mainhome.activity.MainNewActivity.class);
                        break;
                    case "opus"://作品详情
                        String worksId = uri.getQueryParameter("id");
                        if (!TextUtils.isEmpty(worksId)) {
                            VideoListActivity.startActivity(ctx, RecommendFragmentNew.TYPE_SINGLE_WORK,
                                    MultiTypeVideoBean.createSingleVideoParma(Long.parseLong(worksId)));
                        }
                        break;
                    case "topic"://话题详情
                        String topicId = uri.getQueryParameter("id");
                        if (!TextUtils.isEmpty(topicId)) {
                            TopicActivity.startTopicActivity(ctx, Long.parseLong(topicId));
                        }
                        break;
                    case "music"://音乐详情
                        String musicId = uri.getQueryParameter("id");
                        if (!TextUtils.isEmpty(musicId)) {
                            TopicActivity.startMusicActivity(ctx, Long.parseLong(musicId));
                        }
                        break;
                    case "user"://个人主页
                        String userId = uri.getQueryParameter("id");
                        if (!TextUtils.isEmpty(userId)) {
                            navigateToOtherUserHome(ctx, false, Long.valueOf(userId));
                        }
                        break;
                    case "webpage"://打开网页
                        String pageUrl = uri.getQueryParameter("url");   //页面的url
                        String type = uri.getQueryParameter("type");
                        if (!TextUtils.isEmpty(pageUrl)) {
                            if ("1".equalsIgnoreCase(type)) {
                                Intent intent = new Intent();//type=1默认浏览器打开
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setData(Uri.parse(pageUrl));
                                ctx.startActivity(intent);
                            } else {//type=0内嵌浏览器打开
                                WebViewActivity.startWebActivity(ctx, pageUrl, "");
                            }
                        }
                        break;
                    case "message"://进入铃铛消息
                        navigateToBell(ctx);
                        break;
                }
            }
        }
    }

    /**
     * 跳转到消息页面
     *
     * @param context
     */
    public void navigateToBell(Context context) {
        Intent intent = BellsActivity.getCallingIntent(context);
        context.startActivity(intent);
    }

    public static void navigateToWeb(Context context,String url, int type) {
        if (!TextUtils.isEmpty(url)) {
            if (type == 1) {
                Intent intent = new Intent();//type=1默认浏览器打开
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                context.startActivity(intent);
            } else {//type=0内嵌浏览器打开
                WebViewActivity.startWebActivity(context, url, "");
            }
        }
    }
}
